package oops.exception;

import java.io.FileNotFoundException;
import java.io.IOException;



/**
 * 
Exceptions keywords
throw -> to raise new exception <br/>
throws -> pass on an exception instead of handling it<br/>
try{} -> code to be monitored for exceptions<br/>
catch{} -> handle a particle type of exception<br/>
finally{}  -> logic that gets executed irrespective of exception occurs or not.<br/>

checked exceptions -> are the exceptions that extend from Exception class or other definition <br/>is exceptions 
that are not run time exceptions.

unchecked \runtime exceptions -> Exceptions that extend from RuntimeException are called run time <br/>
exceptions or also called unchecked exceptions.

Always handle specific exceptions first and then generic ones.

 * @author harinath
 *
 */
public class ExceptionDemo {

	public static void main(String[] args) throws Exception {
		
		
		try{
		int value = 70/0;
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
	
		Calculator calc = new Calculator();
		
			try {
				System.out.println("adding 10,20 result is " +calc.add(10, 20));
				System.out.println("This line will  be printed since no exception will be thrown");
				int result =70/0;
				
			} 
			catch (MyCheckedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			catch(ArithmeticException ae){
				ae.printStackTrace();
				//throw new Exception(ae);
				throw ae;
			}
			
		    catch(MyRuntimeException | ClassCastException e){
		    	e.printStackTrace();
		    	
		    }
			catch(Exception e){
				e.printStackTrace();
			}
			
			
		
		
		try {
			System.out.println("adding 10,20 result is " +calc.add(-10, 20));
			System.out.println("This line will never be printed since exception will be thrown");
			//clean up here
		} catch (MyCheckedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			System.out.println("This is in finally block and it always gets executed");
		}
		System.out.println("substracting 10,20 result is " +calc.substract(20, 10));

	}

}
